package com.javainterface.dao;

import com.javainterface.utils.Md5;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.StringReader;

/**
 * 获取万户接口的Token
 */
public class GetToken {
    Document doc = null;

    //任一用户账号，可以在OA中不存在
    String curUserAccount = "wanhu";
    //whconfig.xml中serviceKeyList节点下面的name="serviceKey"的值，任一组即可
    String serviceKey = "webappKey";
    //whconfig.xml中serviceKeyList节点下面的name="fixedStr"的值，任一组即可
    String fixedStr = "webapp";

//    String serviceURL = "http://10.0.12.171:7001/defaultroot/xfservices/GeneralWeb",
    String serviceURL = "http://10.0.12.181:8070/defaultroot/xfservices/GeneralWeb",
            serviceTNS = "http://com.whir.service/GeneralWeb", method = "OAManager";

    public String getServiceToken() {
        Long time = System.currentTimeMillis();
        String s = this.serviceKey + "0" + this.curUserAccount + "0" + time + this.fixedStr;
        String sendXml = "<?xml version=\"1.0\" encoding=\"GBK\"?>" +
                "<input>" +
                "<key>" + Md5.getMD5Str("DKezOFFICE12.3") + "</key>" +
                "<cmd>getServiceToken</cmd>" +//产品方法
                "<domain>0</domain>" +
                "<serviceKey>" + this.serviceKey + "</serviceKey>" +
                "<verificationType>0</verificationType>" +
                "<userKey>" + this.curUserAccount + "</userKey> " +
                "<userKeyType>0</userKeyType>" +
                "<time>" + time + "</time>" +
                "<md5key>" + Md5.getMD5Str(s) + "</md5key>" +
                "</input>";
        String backXml = start(sendXml, serviceURL, serviceTNS, method);
        if (backXml.length() > 0) {
            ParseDoc(backXml);
            backXml = getParseNode("data");
        }
        return backXml;
    }

    public String start(String xml, String serviceURL, String serviceTNS, String method) {
        String backXml = "";
        try {
            //String serviceURL = "http://10.82.22.250:8080/services/services/helloWorld";// 调用的web服务的url地址，这里是一个http请求，希望得到的结果是wsdl文档。
            //String serviceTNS = "http://services.lzgt.com";

            org.apache.axis.client.Service service = new org.apache.axis.client.Service();//建立请求服务框架实例。
            org.apache.axis.client.Call call = (org.apache.axis.client.Call) service.createCall();  //从框架中生成一个维护调用的实例。

            call.setTargetEndpointAddress(new java.net.URL(serviceURL));
            call.setOperationName(new javax.xml.namespace.QName(serviceTNS, method));            //设置需要调用的函数名称
            backXml = (String) call.invoke(new Object[]{xml});
        } catch (Exception e) {
            System.out.println("---------调用接口发生错误,访问此地址serviceURL[ " + serviceURL + "?wsdl ]以检查系统是否正常运行----------------");
            e.printStackTrace();
        }
        return backXml;
    }

    public void ParseDoc(String xml) {
        if (xml.length() > 0) {
            xml = xml.replaceAll("\\&lt;", "<").replaceAll("\\&gt;", ">").replaceAll("\\(", "（").replaceAll("\\)", "）");
            // System.out.println(xml);
            try {
                StringReader read = new StringReader(xml);
                if (read != null) {
                    InputSource source = new InputSource(read);
                    if (source != null) {
                        this.doc = new SAXBuilder().build(source);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public String getParseNode(String nodeName) {
        String nodeValue = "";
        if (this.doc != null && nodeName.length() > 0) {
            try {
                Element root = this.doc.getRootElement();            //
                if (root != null && root.getName().length() > 0) {
                    Element eleResult = root.getChild(nodeName);     //
                    if (eleResult != null) {
                        nodeValue = eleResult.getValue();
                        //System.out.println("-------------eleResult--------->>"+nodeValue);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return nodeValue;
    }


}
